package Actions;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


import forms.nameform;

public class changeStatus extends Action
{
	private List<nameform> names = new ArrayList<nameform>();
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		//forms.adminAnaform ad = (forms.adminAnaform) form;
		
		System.out.println(request.getParameter("whichapp"));
		System.out.println(request.getParameter("whatstatus"));
		
		String username = 	request.getParameter("whichapp");	
		String newStatus = request.getParameter("whatstatus");
		
		String WhichPage = (String)(request.getSession().getAttribute("WhichPage"));
		String url = "jdbc:postgresql://localhost:5432/Applications";
		Connection conn = null;
		Statement stmt = null;
		Statement stmt2 = null;

		PreparedStatement ps = null;
		ResultSet rs = null;
		ResultSet rs2 = null;
		String instruc = null;

		
		try {
			// Registering Postgresql JDBC driver
			Class.forName("org.postgresql.Driver");
			// Open a connection to the database	
			conn = DriverManager.getConnection(url,"postgres","postgres");
			// Create the statement
	        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
	                    				ResultSet.CONCUR_UPDATABLE);
	        stmt2 = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
    				ResultSet.CONCUR_UPDATABLE);
	        rs = stmt.executeQuery("SELECT distinct AppID FROM MainTable WHERE NameID ="
	        		+"(SELECT APP FROM users WHERE user_name ='"
					+username+"')");
	        System.out.println("fdfsadfsa:"+username);
	        while(rs.next()){
	        	String updatedstatus = "";
	        	if(newStatus.equals("Reject")){
	        		updatedstatus = "Rejected";
	        	}
	        	else if(newStatus.equals("Accept")){
	        		updatedstatus = "Accepted";
	        	}	 
	        	else{
	        		updatedstatus = "Pending";
	        	}
				instruc = "UPDATE Application SET status = '"+ updatedstatus
				+"' WHERE app_id ='"+rs.getInt("AppID")+"'";
				ps = conn.prepareStatement( instruc );
				ps.executeUpdate();	
	        }
		
		}catch (SQLException e) 
		  { 
			System.out.println(e);
		  } catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
		  return mapping.findForward("changed");
		
		
		}
}
